<template>
	<view class="index">
		<scroll-view scroll-y="true">
			<HeaderNav bgcolor='#56677c' text='衣碳揪镜' :showNavBackBtn='false' color='#fff'></HeaderNav>
			<!-- <NullData /> -->
			<view class="index-container">
				<view class="cn">
					<view class="title">
						<!-- 卓尚服饰有限公司 -->
					</view>
				</view>
				<ProductData :ks="ks" :ksing="ksing" :tSum="tSum" />
				<CarbonData :zSum="zSum" :fSum="fSum" :c="c" />
				<CarbonFootprint :allGoods="allGoods" />
				<CarbonFootprintDischarge />
				<CarbonSequestration :allGoods="allGoods" />
				<CarbonSequestrationDischarge />
				
			</view>
		</scroll-view>
	</view>
</template>

<script>
	import NullData from '@/common/NullData/NullData'
	import ProductData from './components/ProductData'
	import CarbonData from './components/CarbonData'
	import HeaderNav from './components/HeaderNav'
	import CarbonFootprint from './components/CarbonFootprint'
	import CarbonFootprintDischarge from './components/CarbonFootprintDischarge'
	import CarbonSequestration from './components/CarbonSequestration'
	import CarbonSequestrationDischarge from './components/CarbonSequestrationDischarge'
	
	import { getAllGoods as getAllGoodsApi } from '@/api/goods'
	import { getAllRecords as getAllRecordsApi  } from '@/api/emission'
	
	export default {
		data() {
			return {
				cid: 0,
				allGoods: [],
				zSum: 0,
				fSum: 0,
				c: 0,
				allRecords: [],
				ks: 0,
				ksing: 0,
				tSum: 0,
			}
		},
		onShow() {
			this.cid=this.$store.getters.getUser.cid
			if(this.cid==null || this.cid==0) {
				uni.navigateTo({
					url: '../login/login'
				})
			}
			// else {
			// 	this.__getAllGoods()
			// 	this.__getAllRecords()
			// }
			
		},
		// onPullDownRefresh() {
		// 	this.cid=this.$store.getters.getUser.cid
		// 	if(this.cid==null || this.cid==0) {
		// 		uni.navigateTo({
		// 			url: '../login/login'
		// 		})
		// 	}else {
		// 		this.__getAllGoods()
		// 		this.__getAllRecords()
		// 	}
		// },
		created() {
			this.cid=this.$store.getters.getUser.cid
			this.__getAllGoods()
			this.__getAllRecords()
		},
		methods: {
			async __getAllRecords() {
				// console.log(this.cid, typeof this.cid);
				const data ={
					cid: this.cid
				}
				
				const res = await getAllRecordsApi(data)
				if(res.data!=null && res.data.length!=0) {
					this.allRecords=res.data
					let z=0;
					let f=0;
					for(let item of this.allRecords) {
						let carbon=this.getGood(item.gid)
						if(carbon > 0) { // 碳足迹
							z=z+carbon*item.scale
						}else { // 碳固存
							f=f+carbon*item.scale
						}
					}
					
					this.zSum=parseFloat(z).toFixed(1);
					this.fSum=Math.abs(parseFloat(f).toFixed(1));
					this.c=Math.abs(parseFloat(this.zSum-this.fSum).toFixed(1));
					console.log(z,f)
					
					this.getksing()
					this.getks()
				// }else {
				// 	uni.showToast({
				// 		icon: 'error',
				// 		title: '接口调用失败',
				// 		duration: 3000
				// 	})
				// 	console.log('首页')
				}
			},
			getksing() {
				const map = new Map()
				this.ksing=this.allRecords.filter(key => !map.has(key.gid) && map.set(key.gid, 1)).length
				
				// console.log(this.allGoods.length, ss)
			},
			getks() {
				let t=0;
				for(let item of this.allGoods) {
					if(item.carbon > 0) {
						t=t+1;
					}
				}
				this.tSum=t
			},
			getGood(gid) {
				for(let item of this.allGoods) {
					if(item.gid == gid) {
						return item.carbon
					}
				}
			},
			async __getAllGoods() {
				const data ={
					cid: this.cid
				}
				
				const res = await getAllGoodsApi(data)
				// console.log(res)
				if(res.status === this.API_STATUS_CODE.SUCCESS) {
					this.allGoods=res.data
					this.ks=this.allGoods.length
				}else {
					uni.showToast({
						icon: 'error',
						title: '接口调用失败',
						duration: 3000
					})
				}
			}
		},
		components: {
			HeaderNav,
			NullData,
			ProductData,
			CarbonData,
			CarbonFootprint,
			CarbonFootprintDischarge,
			CarbonSequestration,
			CarbonSequestrationDischarge
		}
	}
</script>

<style lang="less" scoped>
	.index {
		
		// width: 100vw;
		height: 100vh;
		
		box-sizing: border-box;
		
		
		.index-container {
			height: 100%;
			padding: 20rpx;
			padding-top: 0;
			
			.cn {
				margin-top: 40rpx;
				// background-color: rgba(255, 255, 255, .1);
				
				.title {
					color: #fff;
					font-size: 47rpx;
					font-weight: 700;
					font-family: '华文楷体', Courier, monospace;
					text-align: center;
				}
			}
		}
		
		scroll-view {
			padding-top: 46rpx;
			// background: linear-gradient(0deg ,#0b2997,#1233a8,#254dd3);
			// background: linear-gradient(to top,#1d2e55,#203f5f,#4f5e72,#68819a);
			background: linear-gradient(to top,#172349,#1d2e55,#203f5f,#4f5e72);
			// background: linear-gradient(to top,#172349,#1d2e55,#203f5f,#4f5e72);
		}
	}
</style>
